package ua.yandex.shad.tempseries;

import sun.java2d.pipe.ValidatePipe;

public class TemperatureSeriesAnalysis {

    private final double[] temperatureSeries;

    public TemperatureSeriesAnalysis(double[] temperatureSeries) throws IllegalTemperatureValueException {
        if (temperatureSeries == null) {
            this.temperatureSeries = new double[]{};
        } else {
            for (double temperature : temperatureSeries) {
                if (temperature < -273) {
                    throw new IllegalTemperatureValueException();
                }
            }
            this.temperatureSeries = temperatureSeries;

        }
    }

    public double calcAverageTemp() {
        if (temperatureSeries.length == 0) {
            return 0;
        }
        double sum = 0;
        for (double temperature : temperatureSeries) {
            sum += temperature;
        }
        return sum / temperatureSeries.length;
    }

    public double calcTempDeviation() {
        if (temperatureSeries.length == 0) {
            return 0;
        }
        double mean = calcAverageTemp();
        double sum = 0;
        for (double temperature : temperatureSeries) {
            sum += Math.pow(temperature - mean, 2.0);
        }
        return sum / temperatureSeries.length;
    }

    public double findTempClosestToZero() {
        return findTempClosestToValue(0);
    }

    public double findTempClosestToValue(double tempValue) {
        if (temperatureSeries.length == 0) {
            return 0;
        }
        double closestTemp = temperatureSeries[0];
        for (double temperature : temperatureSeries) {
            if (Math.abs(tempValue - temperature) == Math.abs(tempValue - closestTemp)) {
                closestTemp = Math.max(temperature, closestTemp);
            } else if (Math.abs(tempValue - temperature) < Math.abs(tempValue - closestTemp)) {
                closestTemp = temperature;
            }
        }
        return closestTemp;
    }

    public double[] findTempsLessThen(double tempValue) {
        if (temperatureSeries.length == 0) {
            return new double[]{};
        }
        double[] lowerTemps;
        int count = 0;
        int index = 0;
        for (double temperature : temperatureSeries) {
            if (temperature < tempValue) {
                count++;
            }
        }
        lowerTemps = new double[count];
        for (double temperature : temperatureSeries) {
            if (temperature < tempValue) {
                lowerTemps[index] = temperature;
                index++;
            }
        }
        return lowerTemps;
    }

    public double[] findTempsGreaterThen(double tempValue) {
        if (temperatureSeries.length == 0) {
            return new double[]{};
        }
        double[] lowerTemps;
        int count = 0;
        int index = 0;
        for (double temperature : temperatureSeries) {
            if (temperature >= tempValue) {
                count++;
            }
        }
        lowerTemps = new double[count];
        for (double temperature : temperatureSeries) {
            if (temperature >= tempValue) {
                lowerTemps[index] = temperature;
                index++;
            }
        }
        return lowerTemps;
    }

    public double[] forecatsTemp() throws NotEnoughValuesToForecastException {
        if (temperatureSeries.length < 10) {
            throw new NotEnoughValuesToForecastException();
        }
        return new double[]{};
    }
}
